package com.ljh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.ljh.common.JsonData;
import com.ljh.mapper.DindingMapper;
import com.ljh.mapper.OutTableMapper;
import com.ljh.pojo.Billdefail;
import com.ljh.pojo.Billtable;
import com.ljh.mapper.BilltableMapper;
import com.ljh.pojo.Dinding;
import com.ljh.pojo.Footthebill;
import com.ljh.service.BILLTABLEervice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljh.utils.ExceptionUtils;
import com.ljh.utils.StringUtils;
import com.ljh.vo.BillVo;
import com.ljh.vo.BilltableVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘剑鸿
 * @since 2021-11-09
 */
@Service
public class BilltableServiceImpl extends ServiceImpl<BilltableMapper, Billtable> implements BILLTABLEervice {

    @Autowired
    private BilltableMapper billtableMapper;

    @Autowired
    private DindingMapper dindingMapper;

    @Autowired
    private OutTableMapper outTableMapper;
    public JsonData SearchBill(BilltableVo billtableVo){
        Page<Billtable> page = PageHelper.startPage(billtableVo.getPage(),billtableVo.getLimit());
        List<Billtable> list = billtableMapper.searchBill(billtableVo);
        if (CollectionUtils.isEmpty(list)){
            list = Lists.newArrayList();
        }
        return JsonData.success(page.getTotal(),list);
    }
    public BillVo<Billtable> getBill(Integer Billid){
        BillVo<Billtable> bi = new BillVo<Billtable>();
        List<Billtable> Bill = billtableMapper.OutBill(Billid);
        Billtable billclient = billtableMapper.BillCi(Billid);//查出会员类型
        if (billclient == null){
            String memberg = "还不是会员";
            bi.setMembergrade(memberg);
        }
        else {
            bi.setMembergrade(billclient.getMembergrade());
            bi.setMemberid(billclient.getMemberid());
            bi.setBalance(billclient.getBalance());
        }

        bi.setList(Bill);

        Bill.stream().forEach(Billtable->{
            BigDecimal big2 = null;
            //System.out.println("钱"+Billtable.getMenuprice());
            Integer IntCount =  Billtable.getConut();
            //System.out.println("数"+IntCount);
            BigDecimal big1 = new BigDecimal(IntCount.toString());
            big2 = big1.multiply(Billtable.getMenuprice());
            //System.out.println("总"+big2);
            Billtable.setZongjin(big2);
        });
        BigDecimal sum = Bill.stream().map(Billtable::getZongjin).reduce(BigDecimal.ZERO,BigDecimal::add);
//        Billtable bi = new Billtable();
        bi.setZonge(sum);
        List<String> list1 = Bill.stream().map(Billtable::getBillnumber).distinct().collect(Collectors.toList());
        List<String> list2 = Bill.stream().map(Billtable::getDiningnumber).distinct().collect(Collectors.toList());
        String result;
        String res;
        result = list1.toString().replace("[", "").replace("]", "").replace(",", "");
        res = list2.toString().replace("[","").replace("]", "").replace(",", "");
        bi.setBillnum(result);
        bi.setDiningnumber(res);

        return bi;
    }
    public Billtable TogetBill(String BillNum){
        Billtable bill = billtableMapper.selectNumOne(BillNum);
        return bill;
    }
    public void AddFoot(Footthebill footthebill){
        String BillNum = footthebill.getBillnumber();
        Footthebill bill = billtableMapper.selectfootOne(BillNum);

        Billtable bi = billtableMapper.selectDinOne(BillNum);
        System.out.println(bi);
        if (bill == null){
            int row = billtableMapper.insetNum(footthebill);

            if (row != 1){
                throw new ExceptionUtils("埋单出错");
            }
            else{
                int DinId = bi.getDiningTableid();
                int OutTableId = bi.getOutTableid();

               // System.out.println("餐桌"+DinId);
                dindingMapper.updateByding(DinId);//修改为空闲
                //删除开台
                outTableMapper.deleteByOutId(OutTableId);
                billtableMapper.deleteById(footthebill.getBillid());
            }
        }
        else{
            throw new ExceptionUtils("该账单已埋单");
        }
    }
}
